package pers.lxl.ajax.server.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import pers.lxl.ajax.server.bean.ResultBean;
import pers.lxl.ajax.server.bean.User;


/**
 * @version v1.0
 * @description: 跨域测试接口 controller
 * @author: lxl
 * @date: 2018/9/8 23:23
 */
@Controller
// @CrossOrigin(allowCredentials = "true")
@RequestMapping("/ajax")
public class AjaxController {

    /**
     * @description: get测试接口
     * @author: lxl
     * @date: 2018/9/8 23:29
     * @param: null
     * @return: ResultBean
     */
    @GetMapping("/test")
    public @ResponseBody
    ResultBean testGet() {
        System.out.println("testGet");
        return new ResultBean("get1 ok");
    }

    /**
     * post测试接口
     *
     * @param user 参数类
     * @return ResultBean
     */
    @PostMapping("/postJson")
    public @ResponseBody
    ResultBean postJson(@RequestBody User user) {
        System.out.println("postJson");
        return new ResultBean(user.getName());
    }

    /**
     * @description: 带cookie的请求
     * @author: lxl
     * @date: 2018/9/9 15:39
     * @param: cookie
     * @return: ResultBean
     */
    @GetMapping("/getCookie")
    public @ResponseBody
    ResultBean getCookie(@CookieValue(value = "cookie1") String cookie1) {
        System.out.println("getCookieRequest1");
        return new ResultBean(cookie1);
    }

    /**
     * @description: 带自定义请求头的请求
     * @author: lxl
     * @date: 2018/9/9 15:55
     * @param: 请求头
     * @return: ResultBean
     */
    @GetMapping("/getCustomHeader")
    public @ResponseBody
    ResultBean getCustomHeader(@RequestHeader("x-header1") String header1, @RequestHeader("x-header2") String header2) {
        System.out.println("getCustomHeader");
        return new ResultBean(header1 + header2);
    }
}
